; /////////////////////////////////////////////////////////////////////////////
;
;     This file is part of PurePicBasic.
; 
;     PurePicBasic is free software: you can redistribute it And/Or modify
;     it under the terms of the GNU General Public License As published by
;     the Free Software Foundation, either version 3 of the License, Or
;     (at your option) any later version.
; 
;     PurePicBasic is distributed in the hope that it will be useful,
;     but WITHOUT ANY WARRANTY; without even the implied warranty of
;     MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
;     GNU General Public License For more details.
; 
;     You should have received a copy of the GNU General Public License
;     along With PurePicBasic.  If Not, see <http://www.gnu.org/licenses/>.
;
; /////////////////////////////////////////////////////////////////////////////

; /////////////////////////////////////////////////////////////////////////////
;
; /////////////////////////////////////////////////////////////////////////////
Procedure _read_def_file_(chipset.s)
  Protected id.i
  
  id = ReadFile(#PB_Any,GetPathPart(ProgramFilename())+"def\"+chipset+".def")
  If id 
    *buffer = AllocateMemory(Lof(id))
    If *buffer 
      ReadData(id,*buffer,Lof(id))
      _parse_def_file_(PeekS(*buffer,Lof(id)))
      FreeMemory(*buffer)
    EndIf
    CloseFile(id)
  Else
    PrintN("Impossible de lire le fichier de definition")
    Exit()
  EndIf
EndProcedure

Procedure _parse_def_file_(string$)
  Protected.a inRegister=#False
 
  string$ = ReplaceString(string$,Chr(13),Chr(10))
  string$ = packString(string$,Chr(10))
  
  string$ = ReplaceString(string$,Chr(9),Chr(32))
  string$ = packString(string$,Chr(32))
  
  NewList lines.s()
  AddElement(lines())
  
  ; Fill file in array
  For i = 1 To Len(string$)
    letter.s = Mid(string$,i,1)
    If letter = Chr(10)
      AddElement(lines())
    Else
      lines() + letter  
    EndIf 
  Next 
  
  ; remove comment
  ForEach lines()
    tmp.s = ""
    delete.b = #False 
    For i = 1 To Len(lines() ) 
      letter.s = Mid(lines(),i,1)
      
      If letter = ";"
        delete = #True 
      EndIf 
      
      If delete = #False
        tmp + letter
      EndIf 
    Next 
    lines() = tmp
  Next 
  
  ; remove empty line
  ForEach lines()
    If lines() = ""
      DeleteElement(lines())
    Else
      lines() = ReplaceString(lines(),Chr(32),"$")
    EndIf
  Next 
  
  ForEach lines()
    Select lines()
        
      Case "#BEGIN_REGISTER"
        inRegister = #True
        
      Case "#END_REGISTER"
        inRegister = #False
        
      Default
        If inRegister 
          Define_Register(lines()) 
        EndIf
        
    EndSelect
  Next
    
EndProcedure

; IDE Options = PureBasic 5.10 (Windows - x86)
; CursorPosition = 38
; FirstLine = 18
; Folding = -
; EnableXP